package dgt;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class CronServlet extends DGTAbstractServlet {
	private final long cacheTimeMs = 5 * 60 * 1000;//5 Min

	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException {
		resp.setContentType("text/plain");
		long start = System.currentTimeMillis();
		try{
			Date old = getKmzCacheDate();
			if(old != null && old.after(new Date(System.currentTimeMillis()-cacheTimeMs))){
				resp.getWriter().write("KMZ not updated (is not old enough)");
				logger.info("KMZ not updated (is not old enough)");
			}else{
				byte[] kmz = grabKmz();
				cacheKmz(kmz);
				resp.getWriter().write("KMZ updated");
			}
		}catch(Exception e){
			logger.warning("Exception catched trying to fetch page: " + e.getMessage());
		}finally{
			logger.info("Elapsed time: "+(System.currentTimeMillis()-start)+"ms.");
		}
	}
}
